/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.jadex.customproject.wizards;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;

import es.upm.dit.gsi.eclipse.jadex.common.Activator;
import es.upm.dit.gsi.eclipse.jadex.common.preferences.PreferenceConstants;
import es.upm.dit.gsi.jadex.customproject.natures.JadexProjectNature;

/**
 * Class that support the creation of a new Jadex project.
 * 
 * It creates a basic java project and adds the Jadex libraries to the classpath.
 * 
 * @author Pablo Muñoz
 */
public class JadexProjectSupport {
	
	public static IProject createProject(String projectName, URI location) {
		if(projectName == null) return null;
		if(!(projectName.trim().length() > 0)) return null;
		try{
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		
		if(!newProject.exists()){
			newProject.create(null);
			newProject.open(null);
			
			IProjectDescription description = newProject.getDescription();
			description.setNatureIds(new String[] { JadexProjectNature.NATURE_ID,JavaCore.NATURE_ID});
			newProject.setDescription(description, null);
			
			IJavaProject javaProject = JavaCore.create(newProject); 

			IFolder binFolder = newProject.getFolder("bin");
			binFolder.create(false, true, null);
			javaProject.setOutputLocation(binFolder.getFullPath(), null);
			
			List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
			
			IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
			LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
			for (LibraryLocation element : locations) {
			 entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
			}
			
			Path jadexPath = new Path(Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.JADEX_PATH));
			String jadexVersion = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.JADEX_VERSION);
			entries.add(JavaCore.newLibraryEntry((IPath) jadexPath.append("/lib/jadex-kernel-bdi-" + jadexVersion + ".jar"), null, null));
			entries.add(JavaCore.newLibraryEntry((IPath) jadexPath.append("/lib/jadex-commons-" + jadexVersion + ".jar"), null, null));
			entries.add(JavaCore.newLibraryEntry((IPath) jadexPath.append("/lib/jadex-platform-base-" + jadexVersion + ".jar"), null, null));
			//Add needed libraries here
						
			//add libs to project class path
			javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
			
			IFolder sourceFolder = newProject.getFolder("src");
			sourceFolder.create(false, true, null);

			IPackageFragmentRoot root2 = javaProject.getPackageFragmentRoot(sourceFolder);
			IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
			IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
			System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
			newEntries[oldEntries.length] = JavaCore.newSourceEntry(root2.getPath());
			javaProject.setRawClasspath(newEntries, null);
		}
		return newProject;
		
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		
		
		return null;
	}
}
